﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dropthings.Util;
using Dropthings.Business.Facade;
using Dropthings.Web.Util;

public partial class Admin_RoleManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SaveClicked(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveRole();
        }
    }

    protected void CancelClicked(object sender, EventArgs e)
    {
        BindList();
    }

    protected void AddNewRoleClicked(object sender, EventArgs e)
    {
        this.Multiview.ActiveViewIndex = 1;
    }

    protected void Items_Selecting(object sender, LinqDataSourceSelectEventArgs args)
    {
        var facade = Services.Get<Facade>();
        {
            args.Result = facade.GetAllRole();
        }
    }

    protected void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        string roleName = e.CommandArgument.ToString();

        if (!string.IsNullOrEmpty(roleName))
        {
            if (string.Equals(e.CommandName, "DeleteItem"))
            {
                //add delete logic here    
                var facade = Services.Get<Facade>();
                {
                    try
                    {
                        facade.DeleteRole(roleName);
                        BindList();
                        WebUtil.ShowMessage(lblMessage, "Role has been successfully deleted.", false);
                    }
                    catch (Exception ex)
                    {
                        WebUtil.ShowMessage(lblMessage, ex.Message, true);
                    }
                }
            }
            else if (string.Equals(e.CommandName, "UserRole"))
            {
                var facade = Services.Get<Facade>();
                {
                    try
                    {
                        lvUser.DataSource = facade.GetAllUsers(new Guid(e.CommandArgument.ToString()));
                        lvUser.DataBind();
                    }
                    catch (Exception ex)
                    {
                        WebUtil.ShowMessage(lblMessage, ex.Message, true);
                    }
                }
                this.Multiview.ActiveViewIndex = 2;

            }
            else if (string.Equals(e.CommandName, "RoleTemplate"))
            {
                var facade = Services.Get<Facade>();
                {
                    try
                    {
                       
                    }
                    catch (Exception ex)
                    {
                        WebUtil.ShowMessage(lblMessage, ex.Message, true);
                    }
                }
                this.Multiview.ActiveViewIndex = 3;
            }
        }
    }

    public void BindList()
    {
        this.lvItems.DataBind();
        this.Multiview.ActiveViewIndex = 0;
    }

    private void SaveRole()
    {
        var facade = Services.Get<Facade>();
        {
            try
            {
                facade.InsertRole(txtRole.Text.Trim());
                BindList();
            }
            catch (ArgumentException ex)
            {
                WebUtil.ShowMessage(lblMessage, ex.Message, false);
            }
        }
    }
    protected void BackClicked(object sender, EventArgs e)
    {
        BindList();
    }
}